<?php


namespace app\admin\controller;


use app\admin\business\noticeBus;
use think\facade\View;

class Notice extends AdminBase
{
    public function index(){
        return View::fetch();
    }
    public function add(){
        return View::fetch();
    }
    public function addNotice(){
        $title = $this->request->param('title');
        $summary = $this->request->param('summary');
        $content = $this->request->param('content');
        $image = $this->request->param('image');
        $data = [
            "title" => $title,
            "summary" => $summary,
            "content" => $content,
            'image' => $image
        ];
        $validate = new \app\admin\validate\Notice();
        if(!$validate->scene('notice_add')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $NoticeBus = new noticeBus();
        try {
            $res = $NoticeBus->addNews($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'添加成功',$res);
    }
    public function Edit(){
        $id = $this->request->param('id');
        View::assign('id',$id);
        return View::fetch();
    }
    public function noticeEdit(){
        $data = $this->request->param("postData");
        $validate = new \app\admin\validate\Notice();
        if(!$validate->scene('notice_update')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $NoticeBus = new noticeBus();
        try {
            $result = $NoticeBus->updateNotice($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'更改成功',$result);
    }
    public function getNoticeDetailById(){
        $id = $this->request->param('id');
        $data = [
            'id'=>$id
        ];
        $validate = new \app\admin\validate\Notice();
        if(!$validate->scene('notice_id')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $NoticeBus = new noticeBus();
        try {
            $result = $NoticeBus->getNoticeDetail($id);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        if(empty($result)){
            return show(config('status.error'),'数据被删除');
        }
        return show(config('status.success'),'查询成功',$result);
    }
    public function noticeList(){
        $current_page = $this->request->param('current_page');
        $num = $this->request->param('num');
        $NoticeBus = new noticeBus();
        try {
            $result = $NoticeBus->getNoticeList($current_page,$num);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'获取成功',$result);
    }
    public function changeStatus(){
        $data = $this->request->param("postData");
        $noticeBus = new noticeBus();
        try {
            $result = $noticeBus->changeStatus($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'更改成功',$result);
    }
    public function removeNoticeById(){
        $id = $this->request->param('id');
        $data = [
            'id'=>$id
        ];
        $noticeBus = new noticeBus();
        try {
            $result = $noticeBus->removeNotice($id);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        if(empty($result)){
            return show(config('status.error'),'数据不存在或者数据被删除');
        }
        return show(config('status.success'),'删除成功',$result);
    }
}